<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
	<title>生产领料</title>
	<script src="../script/mes-import.js"></script>
	<script type="text/javascript">
	$(function() {
		$("#code").focus();
		$("#code").keypress(function(e) {
			// 回车键事件
			if(e.which == 13) {
				search();
			}
		});
	});
	var orderCode = "";
	var dataList = [];
	function search() {
		if ($("#code").val() == "") {
			$("#msg").html("<div style='color: #ff0000'>请先扫描出库单条码！</div>");
			return;
		}
		
		$("#msg").html("");
		orderCode = $("#code").val();
		$.ajax({
			type : 'POST',
			url : "<%=request.getContextPath()%>/outConfirm/search",
			data : {ckd: orderCode},
			success : function(result) {
				if(result.result){
					dataList = result.list;
					var list = result.list;
					var html = [];
					for (var i = 0;i < list.length;i ++) {
						html.push('<tr>');
						html.push('<td>' + list[i].PCODE + '</br>' + list[i].PNAME + '</td>');
						html.push('<td style="color: #f00;">' +
								'<input value="' + Math.abs(list[i].QTY) + '" id="ID_' + list[i].WMS_STORAGE_IN_OUT_ID + 
								'" onkeydown="this.value=this.value.replace(/\D/g, \'\')" style="width: 30px;text-align:center;"/></td>');
						html.push('<td>' + list[i].BATCH_NO + '</td>');
						html.push('<td>' + nvl(list[i].LOCATOR, '') + '</td>');
						html.push('</tr>');
					}
					$("#detailTable").html(html.join(""));
				} else {
					orderCode = "";
					$("#msg").html("<div style='color: #ff0000'>" + result.msg + "</div>");
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert(errorThrown);
			},
			dataType : 'json'
		});
	}
	
	function submit(){
		if (!orderCode) {
			$("#msg").html("<div style='color: #ff0000'>请先扫描出库单条码！</div>");
			return;
		}
		$("#msg").html("");
		var ids = [];
		var qtys = [];
		var sumQty = 0;
		for (var i = 0;i < dataList.length;i ++) {
			var qty = $("#ID_" + dataList[i].WMS_STORAGE_IN_OUT_ID).val() * 1;
			if (isNaN(qty) || qty < 0) {
				$("#msg").html("<div style='color: #ff0000'>" + dataList[i].PCODE + "批次" + 
					dataList[i].BATCH_NO + "数量不合法！</div>");
				return;
			}
			if (qty > Math.abs(dataList[i].QTY)) {
				$("#msg").html("<div style='color: #ff0000'>" + dataList[i].PCODE + "批次" + 
					dataList[i].BATCH_NO + "不能多于需求数量：" + Math.abs(dataList[i].QTY) + "！</div>");
				return;
			}
			ids.push(dataList[i].WMS_STORAGE_IN_OUT_ID);
			qtys.push(qty);
			
			sumQty += qty;
		}
		if (dataList.length > 0 && sumQty == 0) {
			$("#msg").html("<div style='color: #ff0000'>出库数量不能所有都为0！</div>");
			return;
		}
		
		$.ajax({
			type : 'POST',
			url : "<%=request.getContextPath()%>/outConfirm/outStorage",
			data : {code: orderCode, ids: ids.join(","), qtys: qtys.join(",")},
			success : function(responseData) {
				if(responseData.result){
					$("#code").val("");
					$("#detailTable").html("");
					$("#msg").html("<div style='color: #ff0000'>出库成功</div>")
				} else {
					$("#msg").html("<div style='color: #ff0000'>" + responseData.msg + "</div>");
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert(errorThrown);
			},
			dataType : 'json'
		});
	}
	</script>
</head>
<body>
	<table style="width: 100%" cellpadding="3">
		<tr>
			<td colspan="2" style="background-color: #4E81ac;color: #ffffff;">
				<div style="float: left;margin: 2px;font-size: 15px;">拣货出库</div>
				<div style="float: right;margin-right: 10px;" id="buttonDiv">
					<!-- <input onclick="cancel();" style="padding: 5px 10px 5px 10px;" type="button" value="取消"/> -->
				</div>
			</td>
		</tr>
		<tr>
			<td style="width: 70px;" nowrap="nowrap" align="right">出库单号：</td>
			<td style="width: 220px">
				<input type="text" id="code" style="width: 95%" maxlength="32"/>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<table class="detail_table" cellpadding="0" cellspacing="0" border="0">
				<thead>
					<tr>
						<th>物料</th>
						<th>数量</th>
						<th>批次</th>
						<th>货位</th>
					</tr>
				</thead>
				<tbody id="detailTable">
				</tbody>
				</table>
			</td>
		</tr>
		<tr>
			<td style="width: 70px;" align="right">提示：</td>
			<td style="width: 220px" id="msg"></td>
		</tr>
		<tr>
			<td colspan="2" align="center">
				<input type="button" id="submitBtn" class="okButton" value="确定" onclick="javascript:submit();"/>
				&nbsp;&nbsp;
				<input type="button" id="closeBtn" class="cancelButton" value="关闭" onclick="javascript:history.back();"/>
			</td>
		</tr>
	</table>
</body>
</html>